#!/bin/sh
#
# the core program for building MaxWit Linux
#
# http://maxwit.googlecode.com
#
# Authors:
#     Conke Hu    <conke.hu@gmail.com>
#     Tiger Yu    <tigerflying.yu@gmail.com>
#     Fleya Hou   <fleya.hou@gmail.com>
#


THIS_TOP=`dirname $0`

. ${THIS_TOP}/../core/bmw_base


LINUX_PKG_LIST="\
	${MWP_ZLIB} \
	${MWP_E2FS_LIBS} \
	${MWP_NCURSES} \
	${MWP_SYSVINIT} \
	${MWP_UTIL_LINUX} \
	${MWP_COREUTILS} \
	${MWP_DEBIANUTILS} \
	${MWP_UDEV} \
	${MWP_VIM} \
	${MWP_PERL} \
	${MWP_JPEG} \
	${MWP_LIBPNG} \
	${MWP_TIFF} \
	${MWP_LIBUNGIF} \
	${MWP_FREETYPE} \
	${MWP_TSLIB} \
	${MWP_DFB} \
	${MWP_ALSA_LIB} \
	${MWP_ALSA_UTILS} \
	${MWP_LIBMAD} \
	${MWP_LIBID3TAG} \
	${MWP_FBV} \
	${MWP_SDL} \
	${MWP_SDL_IMAGE} \
	${MWP_SDL_TTF} \
	${MWP_SDL_MIXER} \
	${MWP_MPG123} \
	${MWP_MADPLAY} \
	${MWP_MPLAYER} \
	${MWP_PRBOOM} \
	${MWP_DFB_EX} \
	${MWP_LIBUSB} \
	${MWP_USB_UTILS} \
	${MWP_E2FS} \
	"

PKG_UNTESTED="\
	${MWP_GPM} \
	${MWP_MESA_LIB} \
	${MWP_MESA_GLUT} \
	${MWP_MESA_DEMO} \
	"

CheckPkg "${LINUX_PKG_LIST} ${SBP_LIBTOOL} ${MWP_KERNEL}"


MM_FILE="/proc/sys/vm/mmap_min_addr"
MM_MIN_ADDR=`cat ${MM_FILE}` || exit 1
if [ "${MM_MIN_ADDR}" != "0" ]; then
	if [ ${USER} = root ]; then
		echo 0 > ${MM_FILE} || exit 1
	else
		echo "Pls check \"${MM_FILE}\"!"
		exit 1
	fi
fi


BuildSbox2()
{
	local SB2_CONF_PATH=${HOME}/.scratchbox2
	local SB2_CURR_CONF="maxwit-linux"

	echo -n "Initializing rootfs ."

	rm -rf ${ROOTFS_PATH}
	mkdir -p ${ROOTFS_PATH}
	echo -n "."

	cd ${ROOTFS_PATH}

	mkdir -p etc/init.d bin sbin usr/bin usr/include proc sys dev root home tmp
	for i in 1 2 3 4 5 6 7 8
	do
		mkdir -p usr/share/man/man${i}
	done

	cp -a ${SYSROOT_PATH}/lib .
	echo -n "."
	cp -a ${SYSROOT_PATH}/usr/${TARGET_PLAT}/lib usr/
	cp -a ${SYSROOT_PATH}/usr/lib usr/
	echo " OK."

	if [ ${USER} = root ]; then
		[ -e ${ROOTFS_PATH}/dev/console ] || \
			mknod ${ROOTFS_PATH}/dev/console c 5 1

		[ -e ${ROOTFS_PATH}/dev/null ] || \
			mknod ${ROOTFS_PATH}/dev/null c 1 3

		# for i in 1 2 3 4
		# do
		# 	[ -e ${ROOTFS_PATH}/dev/tty${i} ] || \
		#		mknod ${ROOTFS_PATH}/dev/tty${i} c 4 $i	
		# done
	else
		echo "TODO"
	fi


	echo "Initializing scratchbox2 ... "

	rm -rf ${SB2_CONF_PATH}
	mkdir -p ${SB2_CONF_PATH}

	cp ${SRC_PATH}/${SBP_LIBTOOL}.tar.gz ${SB2_CONF_PATH} || exit 1

	cd ${ROOTFS_PATH}
	# sb2-init ${SB2_CURR_CONF} ${TARGET_PLAT}-gcc || exit 1
	SB2_EMU=`which qemu-${TARGET_ARCH}` || exit 1
	echo "sb2: using emulator \"${SB2_EMU}\""
	sb2-init -c ${SB2_EMU} ${SB2_CURR_CONF} ${TARGET_PLAT}-gcc || exit 1
	# ${UTILS_PATH}/bin/sb2-config -d ${SB2_CURR_CONF} || exit 1
	echo


	# bash as a test case :)
	Unpack ${MWP_BASH}

	sb2 ./configure \
		--prefix=/usr \
		--exec-prefix=/ \
		--sysconfdir=/etc \
		|| exit 1

	sb2 make || exit 1

	sb2 -m install make install || exit 1

	ln -sf bash ${ROOTFS_PATH}/bin/sh || exit 1
	cp -v ${THIS_TOP}/pregen/bashrc ${ROOTFS_PATH}/root/.bashrc && \
	cp -v ${THIS_TOP}/pregen/bash_profile ${ROOTFS_PATH}/root/.bash_profile
}

BuildSbox2
echo


SB2_CMD="sb2 -m install"
export CONFIG_SHELL=${UTILS_PATH}/bin/bash


for pkg in ${LINUX_PKG_LIST}
do
	Unpack ${pkg}

	pkg_build=`echo ${pkg%-*} | tr A-Z a-z`

	# I like this bug :)
	cd ${BUILD_PATH}/${pkg}

	if [ -x ${THIS_TOP}/${pkg_build}.sh ]; then
		buildprog=${THIS_TOP}/${pkg_build}.sh
	else
		echo "no building script for ${pkg} found! trying default."
		buildprog=${THIS_TOP}/generic.sh
	fi

	${SB2_CMD} ${buildprog} || exit 1
	echo
done


test "${MM_MIN_ADDR}" != "0" && test ${USER} = root && \
	echo "${MM_MIN_ADDR}" > ${MM_FILE}


# build rootfs images
grep "${ROOTFS_PATH}" /etc/exports > /dev/null 2>&1 || \
{
	if [ ${USER} = root ]; then
		echo "# nfs for maxwit linux" >> /etc/exports
		echo "${ROOTFS_PATH}	*(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports
		exportfs -r
	else
		echo "TODO"
	fi
}


echo
echo "All Done!"
echo
